/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * <p>Project: jdemo202203 - Demo
 * <p>Powered by webrx On 2022-10-28 19:58:38
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Demo {
    public static void main(String[] args) {
        //1 项目添加数据依赖jar文件

        //try{
            //2 加载驱动
            //Class.forName("com.mysql.cj.jdbc.Driver");
        //}catch(Exception e){
        //    e.printStackTrace();
        //}


        try{
            //3 建立连接
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbshop?user=root");

            //4 使用语句对象操作数据库
            PreparedStatement pst = conn.prepareStatement("select id,name from book");
            ResultSet rs = pst.executeQuery();
            while(rs.next()){
                System.out.printf("编号：%d，书籍名称：%s%n",rs.getInt(1),rs.getString(2));
            }
            rs.close();
            pst.close();

            //5 关闭数据库连接
            conn.close();
        }catch(Exception e){
            e.printStackTrace();
        }




    }
}
